package com.iu.control;

import com.google.gson.Gson;
import com.iu.dao.NoteDao;
import com.iu.uitl.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateNote")
public class UpdateNoteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int id = Integer.valueOf(request.getParameter("id"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String noteTime = request.getParameter("noteTime");
        NoteDao noteDao = new NoteDao();
        Result result = new Result();
        Gson gson = new Gson();
        result.isSuccess = noteDao.updateNote(id, title, content, noteTime);
        result.msg = result.isSuccess ? "修改成功" : "修改失败";
        String res=gson.toJson(result);
        response.getWriter().append(res);
    }
}
